/**
 * 
 */
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.MessageBO;
import org.cuatrovientos.erp.model.Message;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;



/**
 * @author Daniel Lacasta
 */
@Controller
public class MessageController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(MessageController.class);

	@Autowired
    private MessageBO messageBO;
    
        /**
         * default constructor
         */
        public MessageController () {
               logger.info("Message BEAN instantiated");
                
        }
        
        
        
        /**
         * handles default /message or /message/list request
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/message","/message/list"})
         */
        @RequestMapping({"/message","/message/list"})
        public ModelAndView messageDefault() {
        	logger.info("default");
 
                Map<String, Object> model = new HashMap<String, Object>();

                List<Message> messageList = messageBO.selectAll();
                model.put("messages", messageList );
                return new ModelAndView("MessageList", model);     
        }



        /**
         * handles delete message, and redirect to message default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/message","/message/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/message/delete"})
        public String deleteMessage(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("delete " + id);

            messageBO.deleteById(id);
            return "redirect:/message";

        }
        
        /**
         * handles new message request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it: in this case "message"
         * @return the name of the view to show
         * @RequestMapping({"/message/new"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/message/new"})
        public ModelAndView newMessage() {
        		logger.info("new message ");
                Message message = new Message();
               
                return new ModelAndView("NewMessage", "message", message);
        }
        
        /**
         * handles new message request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/message/new"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/message/save"})
        public String newMessage(@Valid Message message, BindingResult bindingResult) {
        		logger.info("new message " + message.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("message", message);
        			return "NewMessage";
        		}
        		
        		// If data is ok, insert and go on
                messageBO.insert(message);
                return "redirect:/message";
        }
        
        /**
         * handles delete message, and redirect to message default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/message","/message/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/message/detail"})
        public ModelAndView messageDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("Detail " + id);
            Map<String,Object> model = new HashMap<String,Object>();
            Object message = messageBO.selectById(id);
            model.put("message", message);
            
            return new ModelAndView("MessageDetail",model);

        }
        
        /**
         * handles message update, first part: register loaded
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/message/update"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/message/update"})
        public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("update " + id);
                Map<String,Object> model = new HashMap<String,Object>();
                Object message = messageBO.selectById(id);
                model.put("message", message);
               return new ModelAndView("UpdateMessage",model);

        }
        
        /**
         * handles save update message request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/message/saveUpdate"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/message/saveupdate"})
        public String saveUpdate(@Valid Message message, BindingResult bindingResult) {
        		logger.info("update message " + message.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("message", message);
        			return "UpdateMessage";
        		}
        		
                messageBO.update(message);
                return "redirect:/message";
        }
}
